package com.luo.ams.controller;


import com.example.bbs.front.service.IForAdminService;
import com.luo.ams.mapper.AdminMapper;
import com.luo.ams.security.AdminLoginInfoVo;
import com.luo.ams.security.TokenVO;
import com.luo.ams.service.IAdminService;
import com.luo.bbs.ums.service.IUserForAdminService;
import com.luo.common.restful.JsonResult;
import com.luo.pojo.Announce;
import com.luo.pojo.Building;
import com.luo.pojo.Repair;
import com.luo.pojo.advice.entity.Advice;
import com.luo.pojo.ams.dto.AdminAddDTO;
import com.luo.pojo.ams.dto.AdminLoginDTO;
import com.luo.pojo.ams.entity.Admin;
import com.luo.pojo.ams.vo.AdminInfoVO;
import com.luo.pojo.comment.vo.CommentVO;
import com.luo.pojo.question.vo.QuestionVO;
import com.luo.pojo.tag.entity.Tag;
import com.luo.pojo.ums.entity.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author bin.cn
 * @since 2023-01-19
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService adminService;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @DubboReference
    private IForAdminService forAdminService;
    @DubboReference
    private IUserForAdminService userForAdminService;
    @Autowired
    private AdminMapper adminMapper;

    @PostMapping("/login")
    public JsonResult login(AdminLoginDTO adminLoginDTO) {
        String jwt = adminService.login(adminLoginDTO);
        TokenVO tokenVO = new TokenVO();
        tokenVO.setTokenHead(tokenHead);
        tokenVO.setTokenValue(jwt);
        return JsonResult.ok(tokenVO);
    }

    @PostMapping("/logout")
    public JsonResult logout(@RequestHeader(name = "Authorization") String token) {
        adminService.logout(token);
        return JsonResult.ok();
    }

    /**
     * 管理员信息获取
     *
     * @return
     */
    @GetMapping("/info/get")
    public JsonResult info() {
        AdminInfoVO info = adminService.getInfo();
        return JsonResult.ok(info);
    }


    /**
     * 添加管理员
     *
     * @return
     */
    @PostMapping("/addNew")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult add(AdminAddDTO adminAddDTO) {
        adminService.addNew(adminAddDTO);
        return JsonResult.ok();
    }

    /**
     * 管理员列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult getList() {
        List<AdminInfoVO> list = adminService.getList();
        return JsonResult.ok(list);
    }

    /**
     * 启用或禁用管理员
     */
    @PostMapping("/set/enable")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult setEnable(Admin admin) {
        int enable = adminService.setEnableOrDis(admin);
        return JsonResult.ok(enable);
    }

    /**
     * 删除管理员
     */
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult deleteById(@PathVariable Long id) {
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 通过ID查询管理员
     */
    @GetMapping("/get/one/{id}")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult getOne(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return JsonResult.ok(admin);
    }

    /**
     * 修改角色
     */
    @PostMapping("/update/role")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult updateRole(Admin admin) {
        adminService.updateRole(admin);
        return JsonResult.ok(admin);
    }

    /**
     * 审核帖子
     */
    @PostMapping("/question/check/{id}")
    @PreAuthorize("hasAuthority('/question/manage')")
    public JsonResult checkQuestion(@PathVariable Integer id) {
        int status = forAdminService.checkQuestion(id);
        return JsonResult.ok(status);
    }

    /**
     * 审核评论
     */
    @PostMapping("/comment/check")
    @PreAuthorize("hasAuthority('/comment/manage')")
    public JsonResult checkComment(String id) {
        int status = forAdminService.checkComment(id);
        return JsonResult.ok(status);
    }

    /**
     * 查询所有帖子
     */
    @GetMapping("/question/all")
    @PreAuthorize("hasAuthority('/question/manage')")
    public JsonResult getAll() {
        List<QuestionVO> all = forAdminService.getAll();
        return JsonResult.ok(all);
    }

    /**
     * 查询未审核帖子
     */
    @GetMapping("/question/all/notpass")
    @PreAuthorize("hasAuthority('/question/manage')")
    public JsonResult getAllNotPass() {
        List<QuestionVO> all = forAdminService.getAllNotPass();
        return JsonResult.ok(all);
    }

    /**
     * 查询版块对应帖子
     */
    @GetMapping("/question/get/{tagId}")
    @PreAuthorize("hasAuthority('/question/manage')")
    public JsonResult getByTag(@PathVariable Integer tagId) {
        List<QuestionVO> all = forAdminService.getByTag(tagId);
        return JsonResult.ok(all);
    }

    /**
     * 查询所有评论
     */
    @GetMapping("/comment/all")
    @PreAuthorize("hasAuthority('/comment/manage')")
    public JsonResult getAllComment() {
        List<CommentVO> all = forAdminService.getAllCom();
        return JsonResult.ok(all);
    }

    /**
     * 查询未审核帖子
     */
    @GetMapping("/comment/all/notpass")
    @PreAuthorize("hasAuthority('/question/manage')")
    public JsonResult getAllCommentNotPass() {
        List<CommentVO> all = forAdminService.getAllComNotPass();
        return JsonResult.ok(all);
    }

    /**
     * 根据ID删除帖子
     */
    @PostMapping("/question/delete/{id}")
    @PreAuthorize("hasAuthority('/question/manage')")
    public JsonResult deleteQuestion(@PathVariable Integer id) {
        forAdminService.removeQuestion(id);
        return JsonResult.ok();
    }

    /**
     * 根据ID删除评论
     */
    @PostMapping("/comment/delete")
    @PreAuthorize("hasAuthority('/comment/manage')")
    public JsonResult deleteComment(String id) {
        forAdminService.removeComment(id);
        return JsonResult.ok();
    }

    /**
     * 根据ID删除版块
     */
    @PostMapping("/tag/delete/{id}")
    @PreAuthorize("hasAuthority('/tag/manage')")
    public JsonResult deleteTag(@PathVariable Integer id) {
        forAdminService.removeTag(id);
        return JsonResult.ok();
    }

    /**
     * 添加版块
     */
    @PostMapping("/tag/add")
    @PreAuthorize("hasAuthority('/tag/manage')")
    public JsonResult addTag(Tag tag) {
        forAdminService.addTag(tag);
        return JsonResult.ok();
    }

    /**
     * 编辑版块
     */
    @PostMapping("/tag/edit")
    @PreAuthorize("hasAuthority('/tag/manage')")
    public JsonResult editTag(Tag tag) {
        forAdminService.editTag(tag);
        return JsonResult.ok();
    }

    /**
     * 查询某一条帖子的评论
     */
    @GetMapping("/comment/by/{id}")
    @PreAuthorize("hasAuthority('/comment/manage')")
    public JsonResult getCommentByQ(@PathVariable Integer id) {
        List<CommentVO> commentByQ = forAdminService.getCommentByQ(id);
        return JsonResult.ok(commentByQ);
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/user/list")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getUserList() {
        List<User> users = userForAdminService.userList();
        return JsonResult.ok(users);
    }

    /**
     * 查询每一栋的用户
     */
    @GetMapping("/build/user/list")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getUserListByBuild(String buildingName) {
        List<User> users = userForAdminService.userListByBuild(buildingName);
        return JsonResult.ok(users);
    }

    /**
     * 获取楼栋
     */
    @GetMapping("/building/list")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getBuilding() {
        List<Building> buildings = forAdminService.getBuildings();
        return JsonResult.ok(buildings);
    }

    /**
     * 启用或禁用楼
     */
    @PostMapping("/building/enable")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult enableBuilding(Integer id) {
        int enable = forAdminService.enableBuilding(id);
        return JsonResult.ok(enable);
    }

    /**
     * 添加邀请码
     */
    @PostMapping("/building/add")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult addBuilding(Building building) {
        forAdminService.addBuilding(building);
        return JsonResult.ok();
    }

    /**
     * 启用或禁用管理员
     */
    @PostMapping("/user/set/able/{id}")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult setUserAble(@PathVariable Long id) {
        int able = userForAdminService.setUserAble(id);
        return JsonResult.ok(able);
    }

    /**
     * 处理意见
     */
    @PostMapping("/dispose/advice/{id}")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult disposeAdvice(@PathVariable Integer id) {
        int able = forAdminService.disposeAdvice(id);
        return JsonResult.ok(able);
    }

    /**
     * 获取意见表
     */
    @GetMapping("/advice/list")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getAdvice() {
        List<Advice> adviceList = forAdminService.getAdvice();
        return JsonResult.ok(adviceList);
    }

    /**
     * 获取未处理意见表
     */
    @GetMapping("/advice/list/notpass")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getAdviceNotPass() {
        List<Advice> adviceList = forAdminService.getAdviceNot();
        return JsonResult.ok(adviceList);
    }

    /**
     * 发布公告
     */
    @PostMapping("/announce/post")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult addAnnounce(Announce announce) {
        forAdminService.addAnnounce(announce);
        return JsonResult.ok();
    }

    /**
     * 获取公告表
     */
    @GetMapping("/announce/list")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getAnnounceList() {
        List<Announce> announceList = forAdminService.getAnnounceList();
        return JsonResult.ok(announceList);
    }

    /**
     * 获取报修表
     */
    @GetMapping("/repair/list")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getRepairAll() {
        List<Repair> repair = forAdminService.getRepairAll();
        return JsonResult.ok(repair);
    }
    /**
     * 获取报修表
     */
    @GetMapping("/repair/list/notpass")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult getRepairNotPass() {
        List<Repair> repair = forAdminService.getRepair();
        return JsonResult.ok(repair);
    }

    /**
     * 处理报修
     */
    @GetMapping("/repair/update/{id}")
    @PreAuthorize("hasAuthority('/user/manage')")
    public JsonResult updateRepair(@PathVariable Integer id) {
        int repair = forAdminService.updateRepair(id);
        return JsonResult.ok(repair);
    }
}
